package com.building.hcrm.wcf;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONObject;

import android.content.Context;

import com.building.hcrm.base.BaseParam;
import com.building.hcrm.base.BaseServiceProxy;
import com.building.hcrm.common.ComConst;
import com.building.hcrm.common.DateDeserializer;
import com.building.hcrm.common.HttpHelper;
import com.building.hcrm.model.Achievements;
import com.building.hcrm.model.AchmetLog;
import com.building.hcrm.model.DoSignResult;
import com.building.hcrm.model.PlanDetail;
import com.building.hcrm.model.PlanExamine;
import com.building.hcrm.model.SignLog;
import com.building.hcrm.model.SignParam;
import com.building.hcrm.model.SignQuery;
import com.building.hcrm.model.SysUser;
import com.building.hcrm.model.SysUserParam;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

public class KpiServiceProxy extends BaseServiceProxy
{
	private static KpiServiceProxy _instance;

	public static KpiServiceProxy getInstance(Context context)
	{
		if (_instance == null)
		{
			_instance = new KpiServiceProxy();
		}
		_instance._context = context;
		return _instance;
	}

	public List<Achievements> AchievementsOfUser(String userNum) throws Exception
	{
		List<Achievements> re = null;

		GsonBuilder gsonb = new GsonBuilder();
		DateDeserializer dds = new DateDeserializer();
		gsonb.registerTypeAdapter(Date.class, dds);
		Gson gson = gsonb.create();
		String strPost =String.format("{\"userNum\":\"%1$s\"}", userNum);
		String strReturn = HttpHelper.getInstance(_context).HttpPost(ComConst.SERVICE_URL, "AchievementsOfUser",
				strPost);
		if (ValidateResult(strReturn))
		{
			List<String> lstRe = ProcessResult(strReturn, "AchievementsOfUserResult");
			for(String str : lstRe)
			{
				re = new ArrayList<Achievements>();
				JSONArray ja = new JSONArray(str);
				for (int i = 0; i < ja.length(); i++) 
				{
					Achievements a = new Achievements();
					a = gson.fromJson(ja.getString(i), a.getClass());
					re.add(a);
				}
			}
		}
		return re;
	}
	
	public Boolean AchmetLogSet(List<AchmetLog> param) throws Exception
	{
		Boolean re = false;
		GsonBuilder gsonb = new GsonBuilder();
		DateDeserializer dds = new DateDeserializer();
		gsonb.registerTypeAdapter(Date.class, dds);
		Gson gson = gsonb.create();
		String strPost = gson.toJson(param);
		strPost  = String.format("{\"list\":%1$s}", strPost);
		String strReturn = HttpHelper.getInstance(_context).HttpPost(ComConst.SERVICE_URL, "AchmetLogSet",
				strPost);
		if (ValidateResult(strReturn))
		{
			List<String> lstRe = ProcessResult(strReturn, "AchmetLogSetResult");
			for(String str : lstRe)
			{
				if(null != str && "true".equals(str.toLowerCase()))
				{
					re = true;
				}
			}
		}
		return re;
	}
	
	public List<AchmetLog> AchmetLogOfUser(String userNum, String dateStart,String dateEnd) throws Exception
	{
		List<AchmetLog> re = null;

		GsonBuilder gsonb = new GsonBuilder();
		DateDeserializer dds = new DateDeserializer();
		gsonb.registerTypeAdapter(Date.class, dds);
		Gson gson = gsonb.create();
		String strPost =String.format("{\"userNum\":\"%1$s\",\"dateStart\":\"%2$s\",\"dateEnd\":\"%3$s\"}", userNum,dateStart,dateEnd);
		String strReturn = HttpHelper.getInstance(_context).HttpPost(ComConst.SERVICE_URL, "AchmetLogOfUser",
				strPost);
		if (ValidateResult(strReturn))
		{
			List<String> lstRe = ProcessResult(strReturn, "AchmetLogOfUserResult");
			for(String str : lstRe)
			{
				re = new ArrayList<AchmetLog>();
				JSONArray ja = new JSONArray(str);
				for (int i = 0; i < ja.length(); i++) 
				{
					AchmetLog al = new AchmetLog();
					al = gson.fromJson(ja.getString(i), al.getClass());
					re.add(al);
				}
			}
		}
		return re;
	}
}
